package net.dodoman.thread;

public class Joining {
    public static void main(String args[]) {
	Sleeper s1 = new Sleeper("s1", 1500);// , s2 = new Sleeper("s2", 1500);
	Joiner j1 = new Joiner("j1", s1);// , j2 = new Joiner("j2", s2);
	// s1.interrupt();
    }
}

/**
 * 
 * @author yanping
 * 
 */
class Sleeper extends Thread {

    private int duration;

    public Sleeper(String name, int sleepTime) {
	super(name);
	this.duration = sleepTime;
	start();
    }

    @Override
    public void run() {
	try {
	    System.out.println("sleeper will sleep");
	    sleep(duration);
	} catch (InterruptedException e) {
	    System.out.println(getName() + "is Interrupted");
	}
    }

}

/**
 * 
 * @author yanping
 * 
 */
class Joiner extends Thread {
    private Sleeper sleeper;

    public Joiner(String name, Sleeper sleeper) {
	super(name);
	this.sleeper = sleeper;
	start();
    }

    public void run() {
	try {
	    System.out.println("joinner");
	    sleeper.join();
	} catch (InterruptedException e) {
	    throw new RuntimeException();
	}
    }
}
